package edu.gatech.spacetraderteam3.controller;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import edu.gatech.spacetraderteam3.debug.Debug;
import edu.gatech.spacetraderteam3.model.FileManager;
import edu.gatech.spacetraderteam3.model.Game;
import edu.gatech.spacetraderteam3.model.GameState;
import edu.gatech.spacetraderteam3.model.Resource;
import edu.gatech.spacetraderteam3.model.SpacePlanet;

/**
 * Controller. Allows GUI to interact with the game without knowing the internal workings
 * of the game.
 * 
 * @author Aaron
 * @version 32
 * 
 */
public class GUIEventHandler {
    /**
     * The Current Game being played
     */
    private static Game Game;

    /* INIT */
    /**
     * Sets the game object.
     * 
     * @param game
     *            Game being played.
     */
    public static void setGame(Game game) {
        GUIEventHandler.Game = game;
    }

    /**
     * Gets the Key Listener for the entire game. Currently Only listens for the ESC key.
     * 
     * @return KeyListener obj listening for ESC
     */
    public static KeyListener getKeyListener() {
        return Game == null ? null : new KeyAdapter() {
            public void keyPressed(KeyEvent k) {// TODO: move this to the Game
                if (k.getKeyCode() == KeyEvent.VK_ESCAPE) {
                    Game.toggleMenu();
                }
            }
        };
    }

    /**
     * Saves the game, using FileManager helper class.
     */
    public static void saveGame() {
        FileManager.saveGame(Game.getPlayer());
    }

    /**
     * Loads the game, using FileManager helper class. If no saved game, displays a
     * message.
     */
    public static void loadGame() {
        Game.setPlayer(FileManager.loadGame());
        System.out.println(Game.getPlayer().toString());
    }

    /* Title Screen */
    /**
     * Starts a new game. Just instantiates a new game object using old one.
     */
    public static void newGame() {
        if (Game != null) {
            Game.newGame();
        }
    }

    /**
     * Continues the game. After loading Player, sets them down on Planet and restores
     * data.
     */
    public static void continueGame() {
        if (Game != null) {
            Game.continueGame();
        }
    }

    /* Character Creation Screen */
    /**
     * Makes the character, with info passed from Character Creation screen.
     * 
     * @param name
     *            Name of player
     * @param skills
     *            int[] of skill points
     * @param difficulty
     *            Difficulty of the game
     */
    public static void createCharacter(String name, int[] skills,
            Game.Difficulty difficulty) {
        if (Game != null) {
            Game.startGame(name, skills, difficulty);
        }
    }

    /**
     * Allows the user to exit Character creation screen back to title.
     */
    public static void backToTitle() {
        Game.showTitle();
    }

    /* Planet View */
    /**
     * Writes that the ship tab was pressed.
     */
    public static void shipTabPressed() {
        Debug.write("Ship button pressed", Debug.VERBOSE);
    }

    /**
     * Writes that the cargo tab was pressed.
     */
    public static void cargoTabPressed() {
        Debug.write("Cargo button pressed", Debug.VERBOSE);
    }

    /**
     * Writes that the map button was pressed, and moves the game to the MAPstate.
     */
    public static void mapTabPressed() {
        Debug.write("Map button pressed", Debug.VERBOSE);
        Game.toggleState(GameState.MAP);
    }

    /**
     * Writes that the news tab was pressed. Currently does nothing.
     */
    public static void newsTabPressed() {
        Debug.write("News button pressed", Debug.VERBOSE);
    }

    /**
     * Called when the user buys goods on a planet/from a trader.
     * 
     * @param resource
     *            Resource the player is buying
     * @param amount
     *            Amount of resource being bought
     */
    public static void buy(Resource resource, int amount) {
        if (Game.buyGoods(resource, amount)) {
            Debug.write("Bought " + amount + " " + resource + "s.", Debug.VERBOSE);
        }
    }

    /**
     * Called when the user sells goods on a planet/from a trader.
     * 
     * @param resource
     *            Resource being sold.
     * @param amount
     *            Amount of resource sold.
     */
    public static void sell(Resource resource, int amount) {
        if (Game.sellGoods(resource, amount)) {
            Debug.write("Sold " + amount + " " + resource + "s.", Debug.VERBOSE);
        }
    }

    /**
     * Refuel's the player's ship one FUEL_INCREMENT.
     */
    public static void refuel() {
        Game.refuel();
    }

    /**
     * Upgrades the player's ship's shield one shield category.
     */
    public static void upgradeShield() {
        Game.upgradeShield();
    }

    /**
     * Upgrades the player's ship's weapon one weapon category.
     */
    public static void upgradeWeapon() {
        Game.upgradeWeapon();
    }

    /**
     * Upgrades the player's ship's armor one armor category.
     */
    public static void upgradeArmor() {
        Game.upgradeArmor();
    }
    
    public static void upgradeCargo(){
        Game.upgradeCargo();
    }

    /* Map View */
    /**
     * Lets the user go from the Galaxy view back to the planet view.
     */
    public static void backToPlanet() {
        Game.toggleState(GameState.PLANET);
    }

    /**
     * Travels the player to a new Planet.
     * 
     * @param p
     *            Planet player is traveling to
     */
    public static void travelToPlanet(SpacePlanet p) {
        Game.doEncounter(p);
    }
    
    public static void getPlanetInfo(SpacePlanet planet){
    	Game.sendPlanetInfo(planet);
    }

    /* Encounter View */
    /**
     * Orders the player's ship to attack.
     */
    public static void attack() {
        Debug.write("Attack!", Debug.USER);
        Game.encounterAttack();
    }

    /**
     * Orders the player's ship to flee from the fight.
     */
    public static void flee() {
        Debug.write("Flee!", Debug.USER);
        Game.encounterFlee();
    }

    /**
     * Trades with a trader in space.
     */
    public static void trade() {
        Debug.write("Trade with trader!", Debug.USER);
        Game.trade();
    }

    /**
     * Lets the user decide not to trade with the space trader.
     */
    public static void noTrade() {
        Debug.write("Don't trade...", Debug.USER);
        Game.encounterOver();
    }

    /**
     * User's choice to let the pirates board them. Loses half credits and half of random
     * resource.
     */
    public static void letPiratesBoard() {
        Debug.write("Letting pirates board.", Debug.USER);
        Game.encounterPiratesBoard();
    }

    /**
     * Lets the police board you ship and check for narcotics.
     */
    public static void letPoliceBoard() {
        Debug.write("Let Police Board!", Debug.USER);
        Game.encounterPoliceBoard();
    }

    /**
     * Boring button when no event occurs.
     */
    public static void noEvent() {
        Debug.write("No Event!", Debug.USER);
        Game.encounterOver();
    }

    /**
     * ToString method for GUIEventHandler. Should never really be called, but just in
     * case.
     * 
     * @return String with game's toString.
     */
    public String toString() {
        return "GUIEventHandler with Game: " + Game.toString();
    }
}
